package com.yc.pre.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author baozhuangyu
 * @Date 2021/1/15  16:38
 */
@Component
public class TokenUtil {
    @Resource
    private RedisTemplate redisTemplate;

    /**
     *
     * @param parseObject 更新的token值
     */
    public void updateAllToken(Object parseObject){
        Set<String> keys = redisTemplate.keys(UserUtil.getUserId() + "_" + "*");
        if (!CollectionUtils.isEmpty(keys)){
            keys.forEach(key -> redisTemplate.opsForValue().set(key, JSON.toJSONString(parseObject), redisTemplate.getExpire(key, TimeUnit.SECONDS), TimeUnit.SECONDS));
        }
    }
}
